﻿using System.Configuration;

namespace Eswat.Configuration
{
	/// <summary>
	/// 
	/// </summary>
	public class ServicesCollection : ConfigurationElementCollection
	{
		/// <summary>
		/// Gets the type of the <see cref="T:System.Configuration.ConfigurationElementCollection"/>.
		/// </summary>
		/// <returns>The <see cref="T:System.Configuration.ConfigurationElementCollectionType"/> of this collection.</returns>
		public override ConfigurationElementCollectionType CollectionType
		{
			get
			{
				return ConfigurationElementCollectionType.AddRemoveClearMap;
			}
		}

		/// <summary>
		/// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"/>.
		/// </summary>
		/// <returns>
		/// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
		/// </returns>
		protected override ConfigurationElement CreateNewElement()
		{
			return new ServiceConfigurationElement();
		}

		/// <summary>
		/// Gets the element key for a specified configuration element when overridden in a derived class.
		/// </summary>
		/// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
		/// <returns>
		/// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
		/// </returns>
		protected override object GetElementKey(ConfigurationElement element)
		{
			var serviceElement = (ServiceConfigurationElement)element;

			return serviceElement.Name;
		}

		/// <summary>
		/// Gets or sets the name of the <see cref="T:System.Configuration.ConfigurationElement"/> to associate with the add operation in the <see cref="T:System.Configuration.ConfigurationElementCollection"/> when overridden in a derived class.
		/// </summary>
		/// <returns>The name of the element.</returns>
		///   
		/// <exception cref="T:System.ArgumentException">The selected value starts with the reserved prefix "config" or "lock".</exception>
		public new string AddElementName
		{
			get
			{ return base.AddElementName; }

			set
			{ base.AddElementName = value; }

		}

		/// <summary>
		/// Gets or sets the name for the <see cref="T:System.Configuration.ConfigurationElement"/> to associate with the clear operation in the <see cref="T:System.Configuration.ConfigurationElementCollection"/> when overridden in a derived class.
		/// </summary>
		/// <returns>The name of the element.</returns>
		///   
		/// <exception cref="T:System.ArgumentException">The selected value starts with the reserved prefix "config" or "lock".</exception>
		public new string ClearElementName
		{
			get
			{ return base.ClearElementName; }

			set
			{ base.AddElementName = value; }

		}

		/// <summary>
		/// Gets or sets the name of the <see cref="T:System.Configuration.ConfigurationElement"/> to associate with the remove operation in the <see cref="T:System.Configuration.ConfigurationElementCollection"/> when overridden in a derived class.
		/// </summary>
		/// <returns>The name of the element.</returns>
		/// <exception cref="T:System.ArgumentException">The selected value starts with the reserved prefix "config" or "lock".</exception>
		public new string RemoveElementName
		{
			get
			{ return base.RemoveElementName; }
		}

		/// <summary>
		/// Gets the number of elements in the collection.
		/// </summary>
		/// <returns>The number of elements in the collection.</returns>
		public new int Count
		{
			get { return base.Count; }
		}

		/// <summary>
		/// Gets or sets a property, attribute, or child element of this configuration element.
		/// </summary>
		/// <returns>The specified property, attribute, or child element</returns>
		public ServiceConfigurationElement this[int index]
		{
			get
			{
				return (ServiceConfigurationElement)BaseGet(index);
			}
			set
			{
				if (BaseGet(index) != null)
				{
					BaseRemoveAt(index);
				}
				BaseAdd(index, value);
			}
		}

		/// <summary>
		/// Gets or sets a property, attribute, or child element of this configuration element.
		/// </summary>
		/// <returns>The specified property, attribute, or child element</returns>
		new public ServiceConfigurationElement this[string Name]
		{
			get
			{
				return (ServiceConfigurationElement)BaseGet(Name);
			}
		}

		/// <summary>
		/// Indexes the of.
		/// </summary>
		/// <param name="service">The service.</param>
		/// <returns></returns>
		public int IndexOf(ServiceConfigurationElement service)
		{
			return BaseIndexOf(service);
		}

		/// <summary>
		/// Adds the specified service.
		/// </summary>
		/// <param name="service">The service.</param>
		public void Add(ServiceConfigurationElement service)
		{
			BaseAdd(service);
			// Add custom code here.
		}

		/// <summary>
		/// Adds a configuration element to the <see cref="T:System.Configuration.ConfigurationElementCollection"/>.
		/// </summary>
		/// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to add.</param>
		protected override void BaseAdd(ConfigurationElement element)
		{
			BaseAdd(element, false);
			// Add custom code here.
		}

		/// <summary>
		/// Removes the specified service.
		/// </summary>
		/// <param name="service">The service.</param>
		public void Remove(ServiceConfigurationElement service)
		{
			if (BaseIndexOf(service) >= 0)
				BaseRemove(service.Name);
		}

		/// <summary>
		/// Removes at.
		/// </summary>
		/// <param name="index">The index.</param>
		public void RemoveAt(int index)
		{
			BaseRemoveAt(index);
		}

		/// <summary>
		/// Removes the specified name.
		/// </summary>
		/// <param name="name">The name.</param>
		public void Remove(string name)
		{
			BaseRemove(name);
		}

		/// <summary>
		/// Clears this instance.
		/// </summary>
		public void Clear()
		{
			BaseClear();
			// Add custom code here.
		}
	}
}
